use std::collections::HashMap;

type Polymer = String;
type InsertionRules = HashMap<(char, char), char>;

fn parse_input() -> (Polymer, InsertionRules) {
    let input = std::fs::read_to_string("./day14/input.txt").unwrap();
    
    let polymer = input.lines().nth(0).unwrap().to_string();
    let rules = input.lines().skip(2).map(|line| -> ((char, char), char) {
        (
            (
                line.chars().nth(0).unwrap(), 
                line.chars().nth(1).unwrap()
            ), 
            line.chars().last().unwrap()
        )
    }).collect();
    
    (polymer, rules)
} 

#[allow(unused)]
fn part1(polymer: Polymer, rules: &InsertionRules) {

    fn insert_element(polyer: Polymer, rules: &InsertionRules) -> Polymer {
        let mut new_polyer = String::new();
    
        for (e1, e2) in Iterator::zip(polyer.chars(), polyer.chars().skip(1)) {
            let &insert_element = rules.get(&(e1, e2)).unwrap();
            new_polyer.push(e1);
            new_polyer.push(insert_element);
        }
        new_polyer.push(polyer.chars().last().unwrap());
    
        new_polyer
    }

    let polymer = (0..10).into_iter().fold(polymer, |acc, _| -> Polymer {
        insert_element(acc, rules)
    });

    let mut counter = HashMap::new();
    for element in polymer.chars() {
        let count = counter.entry(element).or_insert(0);
        *count += 1;
    }
    
    let min = counter.values().min().unwrap();
    let max = counter.values().max().unwrap();

    println!("Part1: {}", max - min);
}

#[allow(unused)]
fn part2(polymer: Polymer, rules: &InsertionRules) {
}

fn main() {
    let (polymer, rules) = parse_input();
    part1(polymer.clone(), &rules);
    part2(polymer, &rules);
}
